﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;
using Csla.Data;
using System.Data;
using System.Data.Common;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

namespace Asah.SiteWorx.ProductCatalog
{
    [Serializable()]
    public class CatalogList : Csla.ReadOnlyListBase<CatalogList, CatalogInfo>
    {

        # region Factory Methods

        public static CatalogList GetCatalogList()
        {
            return DataPortal.Fetch<CatalogList>();
        }

        public static CatalogInfo GetCatalogInfo(int catalogId)
        {
            CatalogList catalogList = DataPortal.Fetch<CatalogList>(new SingleCriteria<CatalogList, int>(catalogId));
            CatalogInfo catalogInfo = null;

            if (catalogList.Count > 0)
            {
                catalogInfo = catalogList.Items[0];
            }
            return catalogInfo;
        }
       
        private CatalogList()
        {
            // require use of factory methods
        }

        #endregion

        #region Data Access

        private void DataPortal_Fetch()
        {
            Database db = DatabaseFactory.CreateDatabase("SiteWorx");
            DbCommand dbCommand = db.GetStoredProcCommand("aspnet_ProductCatalogs_GetProductCatalogs");
            db.AddInParameter(dbCommand, "ApplicationId", DbType.Guid, Site.GetSite().ApplicationId);

            try
            {
                using (var dr = new SafeDataReader(db.ExecuteReader(dbCommand)))
                {
                    RaiseListChangedEvents = false;
                    IsReadOnly = false;

                    while (dr.Read())
                    {
                          CatalogInfo info = new CatalogInfo(dr.GetInt32("ProductCatalogId"), dr.GetString("Name"), dr.GetString("Description"),
                                                             dr.GetString("CoverProductImagePath"), dr.GetGuid("CreatedBy"), dr.GetSmartDate("CreatedDate"),
                                                             dr.GetGuid("LastUpdatedBy"), dr.GetSmartDate("LastUpdatedDate"));
                          this.Add(info);
                    }
                    IsReadOnly = true;
                    RaiseListChangedEvents = true;
                }
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, "Data Access Layer Policy");
                if (rethrow)
                {
                    throw;
                }
            }

        }

        private void DataPortal_Fetch(SingleCriteria<CatalogList, int> criteria)
        {
            Database db = DatabaseFactory.CreateDatabase("SiteWorx");
            DbCommand dbCommand = db.GetStoredProcCommand("aspnet_ProductCatalogs_GetProductCatalog");
            db.AddInParameter(dbCommand, "ApplicationId", DbType.Guid, Site.GetSite().ApplicationId);
            db.AddInParameter(dbCommand, "ProductCatalogId", DbType.Int32, criteria.Value);

            try
            {
                using (var dr = new SafeDataReader(db.ExecuteReader(dbCommand)))
                {
                    RaiseListChangedEvents = false;
                    IsReadOnly = false;

                    if (dr.Read())
                    {
                        
                        CatalogInfo info = new CatalogInfo();
                        info.ID = dr.GetInt32("ProductCatalogId");
                        info.Name = dr.GetString("Name");
                        info.Description = dr.GetString("Description");
                        info.CoverProductImagePath = dr.GetString("CoverProductImagePath");
                        info.CreatedBy = Membership.User.GetUser(dr.GetGuid("CreatedBy")); 
                        info.CreatedDate = dr.GetSmartDate("CreatedDate");
                        info.LastUpdatedBy = Membership.User.GetUser(dr.GetGuid("LastUpdatedBy"));
                        info.LastUpdatedDate = dr.GetSmartDate("LastUpdatedDate");
                        
                        dr.NextResult();
                        if (dr.Read())
                        {
                            info.CoverProductImagePath = dr.GetString("ImagePath");
                        }

                        this.Add(info);
                    }
                    IsReadOnly = true;
                    RaiseListChangedEvents = true;
                }
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, "Data Access Layer Policy");
                if (rethrow)
                {
                    throw;
                }
            }

        }

        #endregion

    }
}
